package com.baizhi.mall.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baizhi.mall.entity.mysql.Admin;
import com.baizhi.mall.service.AdminService;
import com.baizhi.mall.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class AdminController {
    @Autowired
    private AdminService as;
    @Autowired
    private BCryptPasswordEncoder bpe;
    @GetMapping("/admin")
    @PreAuthorize("hasAnyAuthority('sys:user:list,sys:user:info')")
    public ResultEntity <PageInfo<Admin>>selectByPage(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "5")int pageSize){
        //获取用户信息
        PageInfo<Admin> pageInfo = as.selectByPage(pageNum,pageSize);
        return ResultEntity.ok(pageInfo);
    }
    @DeleteMapping("/admin/{id}")
    @PreAuthorize("hasAnyAuthority('sys:user:delete')")
    public ResultEntity<String> delete(@PathVariable("id")Integer id){
        as.delete(id);
        return ResultEntity.ok();
    }
    @PostMapping("/admin")
    @PreAuthorize("hasAnyAuthority('sys:user:save,sys:role:selete')")
    public ResultEntity<String>insert(@RequestParam Admin admin){
        as.insert(admin);
        return ResultEntity.ok();
    }
    @PutMapping("/admin")
    @PreAuthorize("hasAnyAuthority('sys:user:update,sys:role:select')")
    public ResultEntity<String> update(@RequestParam Admin admin){
        as.update(admin);
        return ResultEntity.ok();
    }
    @GetMapping("/status")
    @PreAuthorize("hasAnyAuthority('sys:user:update,sys:role:select')")
    public ResultEntity<String> updateStatus(Integer status,Integer id){
        as.updateStatus(status,id);
        return ResultEntity.ok();
    }
    //注册
    @PostMapping("register")
    @PreAuthorize("hasAnyAuthority('sys:user:save,sys:role:select')")
    public ResultEntity<String> register(@RequestParam Admin admin){
        admin.setPassword(bpe.encode(admin.getPassword()));
        as.insert(admin);
        return ResultEntity.ok();
    }
    @PostMapping("/login")
    public ResultEntity login(@RequestBody Admin admin, HttpSession session){
        Admin ad = as.selectOne(admin.getUsername());
        if(admin != null) {
            if (session.getAttribute("realCode").equals(admin)){
                session.setAttribute("username",admin.getUsername());
                return ResultEntity.ok();
            }
        }

        return ResultEntity.error(1000,"用户不存在");
    }
    @GetMapping("code")
    public void authCode(HttpServletResponse res, HttpSession session){
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(195, 60);
        String code = lineCaptcha.getCode();
        System.out.println("code = " + code);
        session.setAttribute("realCode",code);
        try {
            lineCaptcha.write(res.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
